import { ElMessageBox, ElMessage, ElLoading } from "element-plus";
export type MessageType = "" | "success" | "warning" | "info" | "error";

/**
 *
 * @param api  请求函数
 * @param params 参数
 * @param name 操作名称  如： 删除，退回，送审，生成
 * @param count 数量
 * @param confirmType 弹窗类型
 * @param msg 弹窗内容
 * @param desc 弹窗小字部分说明
 * @param resMsg 操作成功提示
 * @returns
 * 例： 大字：确定要删除目标项吗？ 小字：【删除后可能的影响】
 * 大字：确定要删除这【n】项吗？   小字：【删除后可能的影响】
 */
export const confirmMsgBox = (
  api: (params: any) => Promise<any> | any,
  params: any = {},
  name: string,
  count: number = 1,
  confirmType: MessageType = "warning",
  msg?: string,
  desc?: string,
  resMsg?: string
) => {
  if (!msg) {
    if (desc) {
      desc = `<span class="msg-desc">${desc}</span>`;
    } else {
      desc = "";
    }
    if (count == 0) {
      msg = `未选中默认全部，确定要${name}所有项吗？${desc}`;
    } else if (count == 1) {
      msg = `确定要${name}目标项吗？${desc}`;
    } else if (count == -99) {
      msg = `确定要${name}所有项吗？${desc}`;
    } else {
      msg = `确定要${name}这${count}项吗？${desc}`;
    }
  }
  return new Promise((resolve, reject) => {
    ElMessageBox.confirm(msg, "", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: confirmType,
      autofocus: false,
      dangerouslyUseHTMLString: true
    })
      .then(async () => {
        const res = await api(params);
        if (!res) return reject(false);
        let tip = `${name}成功!`;
        if (resMsg) {
          tip = resMsg;
        }
        ElMessage({
          type: "success",
          message: tip
        });
        resolve(true);
      })
      .catch(() => {});
  });
};

/**
 * 获取选中数据关键词集合及拦截校验
 * @param arr 选中数据
 * @param isNeedIC 是否需要拦截
 * @param isSingle 是否单选
 * @param keyName 关键词名称
 * @returns
 */
export const getCheckIdsAndIC = (arr = [] as any[], isNeedIC = false, isSingle = false, keyName = "id") => {
  let ids = arr.map((el: any) => el[keyName]);
  if (isNeedIC) {
    let msg = "";
    if (isSingle && ids.length != 1) {
      msg = `请选择一条数据进行操作!`;
    } else if (ids.length == 0) {
      msg = `至少选择一条数据进行操作!`;
    }
    if (msg) {
      ElMessage({ type: "warning", message: msg });
      return {
        isPass: false,
        ids: ids
      };
    }
  }
  return {
    isPass: true,
    ids: ids
  };
};

/* 全局请求 loading */
let loadingInstance: ReturnType<typeof ElLoading.service>;

/**
 * @description 开启 Loading
 * */
const startLoading = () => {
  loadingInstance = ElLoading.service({
    fullscreen: true,
    lock: true,
    text: "加载中",
  });
};

/**
 * @description 结束 Loading
 * */
const endLoading = () => {
  loadingInstance.close();
};

/**
 * @description 显示全屏加载
 * */
let needLoadingRequestCount = 0;
export const showFullScreenLoading = () => {
  if (needLoadingRequestCount === 0) {
    startLoading();
  }
  needLoadingRequestCount++;
};

/**
 * @description 隐藏全屏加载
 * */
export const tryHideFullScreenLoading = () => {
  if (needLoadingRequestCount <= 0) return;
  needLoadingRequestCount--;
  if (needLoadingRequestCount === 0) {
    endLoading();
  }
};
